// 写路由守卫和路由跳转的文件
// import { ItemChildrenWrap } from 'antd-mobile/es/components/dropdown/item';
import React, { PureComponent } from 'react';
//, withRouter
import { Redirect, Route } from 'react-router-dom';
import { connect } from 'react-redux';
class RouterView extends PureComponent {
    render() {
        const { routes, location, token } = this.props;
        //当前页面链接
        const { pathname } = location;
        const isLogin = token !== '' ? true : false;  //登录状态可以保存到 redux 、缓存中
        const targetRouterConfig = routes.find(item => {
            return item.path.replace(/\s*/g, "") === pathname ||
                item.name === pathname.substr(0, pathname.lastIndexOf("/"))
        })
        if (targetRouterConfig === undefined) {
            return <Redirect to="/page404" />
        }
        const { component, path } = targetRouterConfig;
        if (targetRouterConfig && !targetRouterConfig.auth && !isLogin) {
            return <Route path={path} component={component}></Route>
        }
        if (isLogin) {
            return <Route path={path} component={component}></Route>
        }
        else {
            if (targetRouterConfig && targetRouterConfig.auth) {
                return <Redirect to="/login" />
            }
        }
    }
}
export default connect(
    state => ({ token: state.user.token || '' })
)(RouterView);